﻿/**
 * 
 */
package com.alstudio.ui.module.account;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alstudio.module.xmpp.protocol.xmpp.data.ALXmppData;
import com.alstudio.module.xmpp.request.ALUiXmppRequestCreator;
import com.alstudio.pojo.ALXmppEvent;
import com.alstudio.ui.base.BaseActivity;
import com.alstudio.ui.module.main.MainActivity;
import com.alstudio.utils.encode.ALDES;
import com.alstudio.utils.log.ALLog;
import com.alstudio.view.PageIndicator;
import com.alstudio.view.PagedAdapter;
import com.alstudio.view.PagedView;
import com.alstudio.view.PagedView.OnPagedViewChangeListener;
import com.loovee.imaohu.R;

/**
 * 此界面用于显示登录、注册、QQ登录
 * 
 * @author Alonso Lee
 * 
 */
public class GetAccountActivity extends BaseActivity implements OnClickListener {

	private final String FAILED_TRY = "retryfailed";

	private int PAGE_COUNT = 7;

	private int[] bgResource = { R.drawable.landed_1, R.drawable.landed_2,
			R.drawable.landed_3, R.drawable.landed_4, R.drawable.landed_5,
			R.drawable.landed_6, R.drawable.landed_7 };

	private int[] bgText = { R.string.TxtAppInfoTipsTitle1,
			R.string.TxtAppInfoTipsTitle2, R.string.TxtAppInfoTipsTitle3,
			R.string.TxtAppInfoTipsTitle4, R.string.TxtAppInfoTipsTitle5,
			R.string.TxtAppInfoTipsTitle6, R.string.TxtAppInfoTipsTitle7 };

	private int[] bgTextButtom = { R.string.TxtAppInfoTipsDescribe1,
			R.string.TxtAppInfoTipsDescribe2, R.string.TxtAppInfoTipsDescribe3,
			R.string.TxtAppInfoTipsDescribe4, R.string.TxtAppInfoTipsDescribe5,
			R.string.TxtAppInfoTipsDescribe6, R.string.TxtAppInfoTipsDescribe7 };

	private PageIndicator mPageIndicator;

	private TextView titleText, contentMiddle;

	private boolean isReqeustThirdPartLogin = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.get_account);
		setContentRes(R.layout.get_account);
		initUI();
		initData();
		setupView(this, getClass().getSimpleName());
	}

	@Override
	public void onBackPressed() {
		// 按下返回键，退回首页
		backToSystemHome();
	}

	/**
	 * 初始化UI
	 */
	private void initUI() {

		// 登陆按钮
		findViewById(R.id.ll_towards_login).setOnClickListener(this);
		// 注册按钮
		findViewById(R.id.bt_register).setOnClickListener(this);
		// qq登陆按钮
		findViewById(R.id.ll_tencent_login).setOnClickListener(this);
		// 微博登录
		findViewById(R.id.ll_sina_login).setOnClickListener(this);

		PAGE_COUNT = bgResource.length;

		final PagedView pagedView = (PagedView) findViewById(R.id.pageView);
		pagedView.setOnPageChangeListener(mOnPagedViewChangedListener);
		pagedView.setAdapter(new GetAccountAdapter());

		mPageIndicator = (PageIndicator) findViewById(R.id.page_indicator);
		mPageIndicator.setDotCount(PAGE_COUNT);

		titleText = (TextView) findViewById(R.id.tv_title_up);

		contentMiddle = (TextView) findViewById(R.id.tv_content_middle);

	}

	// ============================================8.30加==============================================
	private OnPagedViewChangeListener mOnPagedViewChangedListener = new OnPagedViewChangeListener() {

		@Override
		public void onStopTracking(PagedView pagedView) {
		}

		@Override
		public void onStartTracking(PagedView pagedView) {
		}

		@Override
		public void onPageChanged(PagedView pagedView, int previousPage,
				int newPage) {
			setActivePage(newPage);

			// 图片切换
			titleText.setText(bgText[newPage]);
			contentMiddle.setText(bgTextButtom[newPage]);
		}
	};

	private void setActivePage(int page) {
		mPageIndicator.setActiveDot(page);
	}

	private class GetAccountAdapter extends PagedAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return PAGE_COUNT;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ImageView item = null;

			if (convertView == null) {
				item = new ImageView(GetAccountActivity.this);

			} else {
				item = (ImageView) convertView;
			}

			item.setBackgroundResource(bgResource[position]);

			return item;
		}
	}

	// ===============================8.30加=====================================

	@Override
	public void onClick(View v) {
		Intent i = new Intent();

		switch (v.getId()) {
		case R.id.ll_towards_login:
			i.setClass(this, LoginActivity.class);
			startActivity(i);
			break;
		case R.id.bt_register:
			i.setClass(this, RegisterActivity.class);
			startActivity(i);

			break;
		case R.id.ll_tencent_login:
			doTencentLogin();
			break;
		case R.id.ll_sina_login:
			doSinaLogin();
			break;
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.ui.base.BaseActivity#handleLoginedEvent(com.alstudio.pojo
	 * .ALXmppEvent)
	 */
	@Override
	public void handleLoginedEvent(ALXmppEvent data) {
		// TODO Auto-generated method stub
		if (isReqeustThirdPartLogin) {
			// 第三方登陆且是在这个界面执行登陆的，跳转到主界面
			ALLog.d("第三方登陆且是在这个界面执行登陆的，跳转到主界面");
			isReqeustThirdPartLogin = false;
			startActivity(new Intent(this, MainActivity.class));
		} else {
			ALLog.d("再别的地方登陆了，关闭此界面");
		}
		finish();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.ui.base.BaseActivity#handleCheckThirdPartLoginStateEvent
	 * (com.alstudio.pojo.ALXmppEvent)
	 */
	@Override
	public void handleCheckThirdPartLoginStateEvent(ALXmppEvent data) {
		// TODO Auto-generated method stub
		super.handleCheckThirdPartLoginStateEvent(data);
		if (TextUtils.isEmpty(data.getJid())) {
			// 没注册过，需要跳转界面
			Intent i = new Intent(this, EnterPersonInfoActivity.class);
			startActivity(i);
		} else {
			// 已经注册了，直接登陆
			String jid = data.getJid();
			String pwd = data.getPwd();
			try {
				jid = ALDES.decryptDES(jid, ALXmppData.DES_KEY);
				pwd = ALDES.decryptDES(pwd, ALXmppData.DES_KEY);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			ALLog.d("第三方账号已经注册过 开始登陆  " + jid + "密码 " + pwd);
			isReqeustThirdPartLogin = true;
			ALUiXmppRequestCreator.postALUiXmppLoginEvent(jid, pwd);

		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.ui.base.BaseActivity#handleAuthFailedEvent(java.lang.String)
	 */
	@Override
	public void handleAuthFailedEvent(String errorMessage) {
		// TODO Auto-generated method stub
		super.handleAuthFailedEvent(errorMessage);
		isReqeustThirdPartLogin = false;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#initData()
	 */
	@Override
	protected void initData() {
		// TODO Auto-generated method stub

	}

}
